class RecursiveCharacterTextSplitter:
    def __init__(self, chunk_size=1000, chunk_overlap=200, separators=None):
        self.chunk_size = chunk_size
        self.chunk_overlap = chunk_overlap
        if separators is None:
            self.separators = ["\n\n", "\n", " ", ""]
        else:
            self.separators = separators

    def split_text(self, text):
        # 定义递归分割的内部函数
        def recursive_split(text, separators):
            # 如果分割符列表为空，直接按块大小切分
            if not separators:
                return [
                    text[i : i + self.chunk_size]
                    for i in range(0, len(text), self.chunk_size)
                ]
            # 取第一个分割符进行分割
            sep = separators[0]
            # 按当前分割符切分文本
            parts = text.split(sep)
            # 初始化结果列表
            result = []
            # 遍历每一段
            for part in parts:
                # 如果不是最后一段，则补回分隔符
                if part != parts[-1]:
                    part += sep
                # 如果当前段落长度大于块大小，递归用下一个分隔符继续分割
                if len(part) > self.chunk_size:
                    result.extend(recursive_split(part, separators[1:]))
                else:
                    # 否则直接加入结果
                    result.append(part)
            # 返回分割结果
            return result

        splits = [s for s in recursive_split(text, self.separators) if s.strip()]
        print("splits", splits)
        chunks = []
        # 初始化索引
        i = 0
        # 遍历所有分割结果
        while i < len(splits):
            # 当前分块
            chunk = splits[i]
            # 下一个分块的索引
            j = i + 1
            # 尽量合并多个分块到一个chunk，直到超出块大小
            while j < len(splits) and len(chunk) + len(splits[j]) <= self.chunk_size:
                chunk += splits[j]
                j += 1
            # 添加合并后的分块到结果列表
            chunks.append(chunk)
            # 如果需要重叠，并且还有剩余分块
            if self.chunk_overlap > 0 and j < len(splits):
                # 获取重叠部分
                overlap = chunk[-self.chunk_overlap :]
                # 将重叠部分加到下一个分块前面
                if j < len(splits):
                    splits[j] = overlap + splits[j]
            # 移动到下一个分块
            i = j
        # 返回所有分块
        return chunks


r_splitter = RecursiveCharacterTextSplitter(
    chunk_size=5,
    chunk_overlap=10,
    separators=["\n\n", "\n", ".", "。", "，"],
)

text = """
这是第一句话。
这是第二句话，包含中文逗号，
This is the first English sentence.
This is the second English sentence.
这是第三句话.
这是第四句话，包含英文逗号,

这是第五句话，段落分隔。
"""

chunks = r_splitter.split_text(text)
for i, chunk in enumerate(chunks):
    print(f"Chunk {i+1}:\n{chunk}\n")
